package com.ti.demo.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @description: com.ti.demo.controller
 * @Author Triagen
 * @Date 2025/7/18 10:53
 */
@RequestMapping("/prompt")
@RestController
public class ChatController {

    /**
     * Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.
     * <p>
     * Explain the above in one sentence:
     */
    private static final PromptTemplate TEXT_SUMMARIZATION_PROMPT_TEMPLATE = new PromptTemplate("""
            {text}
                        
            Explain the above in one sentence:
            """);

    /**
     * Author-contribution statements and acknowledgements in research papers should state clearly and specifically whether, and to what extent, the authors used AI technologies such as ChatGPT in the preparation of their manuscript and analysis. They should also indicate which LLMs were used. This will alert editors and reviewers to scrutinize manuscripts more carefully for potential biases, inaccuracies and improper source crediting. Likewise, scientific journals should be transparent about their use of LLMs, for example when selecting submitted manuscripts.
     * <p>
     * Mention the large language model based product mentioned in the paragraph above:
     */
    private static final PromptTemplate INFORMATION_EXTRACTION = new PromptTemplate("""
            {text}
                        
            {extract}
            """);

    /**
     * Answer the question based on the context below. Keep the answer short and concise. Respond "Unsure about answer" if not sure about the answer.
     * <p>
     * Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.
     * <p>
     * Question: What was OKT3 originally sourced from?
     * <p>
     * Answer:
     */
    private static final PromptTemplate QUESTION_ANSWERING_PROMPT_TEMPLATE = new PromptTemplate("""
            Answer the question based on the context below. Keep the answer short and concise. Respond "Unsure about answer" if not sure about the answer.

            Context: {context}

            Question: {question}

            Answer:
            """);

    /**
     * Classify the text into neutral, negative or positive.
     * <p>
     * Text: I think the vacation is okay.
     * Sentiment: neutral
     * <p>
     * Text: I think the food was okay.
     * Sentiment:
     */
    private static final PromptTemplate TEXT_CLASSIFICATION_PROMPT_TEMPLATE = new PromptTemplate("""
            {instruction}
                        
            {text}
            """);

    /**
     * The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.
     *
     * Human: Hello, who are you?
     * AI: Greeting! I am an AI research assistant. How can I help you today?
     * Human: Can you tell me about the creation of black holes?
     * AI:
     *
     * The following is a conversation with an AI research assistant. The assistant answers should be easy to understand even by primary school students.
     *
     * Human: Hello, who are you?
     * AI: Greeting! I am an AI research assistant. How can I help you today?
     * Human: Can you tell me about the creation of black holes?
     * AI:
     */
    private static final PromptTemplate CONVERSATION_PROMPT_TEMPLATE = new PromptTemplate("""
            The following is a conversation with an AI research assistant. The assistant answers should be easy to understand even by primary school students.
                        
            {conversation}
            """);

    @Qualifier("deepSeekChatClient")
    @Autowired
    private ChatClient deepSeekChatClient;


    @GetMapping(value = "/textSummarization")
    Flux<String> textSummarization(String text) {
        return this.deepSeekChatClient.prompt()
                .user(user -> user.text(TEXT_SUMMARIZATION_PROMPT_TEMPLATE.getTemplate()).param("text", text))
                .stream()
                .content();
    }

    @GetMapping(value = "/informationExtraction")
    Flux<String> informationExtraction(String text, String extract) {
        return this.deepSeekChatClient.prompt()
                .user(user -> user.text(INFORMATION_EXTRACTION.getTemplate()).param("text", text).param("extract", extract))
                .stream()
                .content();
    }

    @GetMapping(value = "/questionAnswering")
    Flux<String> questionAnswering(String context, String question) {
        return this.deepSeekChatClient.prompt()
                .user(user -> user.text(QUESTION_ANSWERING_PROMPT_TEMPLATE.getTemplate()).param("context", context).param("question", question))
                .stream()
                .content();
    }

    @GetMapping(value = "/textClassification")
    Flux<String> textClassification(String instruction, String text) {
        return this.deepSeekChatClient.prompt()
                .user(user -> user.text(TEXT_CLASSIFICATION_PROMPT_TEMPLATE.getTemplate()).param("instruction", instruction).param("text", text))
                .stream()
                .content();
    }

    @GetMapping(value = "/conversation")
    Flux<String> conversation(String conversation) {
        return this.deepSeekChatClient.prompt()
                .user(user -> user.text(CONVERSATION_PROMPT_TEMPLATE.getTemplate()).param("conversation", conversation))
                .stream()
                .content();
    }

    @GetMapping(value = "/codeGeneration")
    Flux<String> codeGeneration(String codeGeneration) {
        return this.deepSeekChatClient.prompt()
                .user(codeGeneration)
                .stream()
                .content();
    }

    @GetMapping(value = "/reasoning")
    Flux<String> reasoning(String reasoning) {
        return this.deepSeekChatClient.prompt()
                .user(reasoning)
                .stream()
                .content();
    }

}
